using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public enum FriendSystemType
    {
        None = 0,
        friendList,
        friendApplication,
        friendSearch,
        friendShield,
    }
    public class UIFriend : UIBaseWindow
    {

        #region Template Generate,don't modify
        protected class UIB_UIFriend
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject topLeftBack { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleGroup { protected set; get; }
            public UnityEngine.GameObject friend_List { protected set; get; }
            public UnityEngine.GameObject friend_Application { protected set; get; }
            public UnityEngine.GameObject friend_Search { protected set; get; }
            public UnityEngine.GameObject friend_Shield { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv0);
                this.topLeftBack = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleGroup", out var __tbv1);
                this.toggleGroup = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("friend_List", out var __tbv2);
                this.friend_List = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("friend_Application", out var __tbv3);
                this.friend_Application = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("friend_Search", out var __tbv4);
                this.friend_Search = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("friend_Shield", out var __tbv5);
                this.friend_Shield = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIFriend),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private int IndexCurrTab = 0;
        private UIFriend_List uiFriendList = null;
        private UIFriend_Application uiFriendApplication = null;
        private UIFriend_Search uIFriend_Search = null;
        private UIFriend_Shield uIFriendShield = null;
        private List<ExToggle> tabs = new(4);
        #endregion fields

        #region properties

        protected UIB_UIFriend ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIFriend();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
            uiFriendList = this.InitChildNode<UIFriend_List>(this.ui.friend_List);
            uiFriendApplication = this.InitChildNode<UIFriend_Application>(this.ui.friend_Application);
            uIFriend_Search = this.InitChildNode<UIFriend_Search>(this.ui.friend_Search);
            uIFriendShield = this.InitChildNode<UIFriend_Shield>(this.ui.friend_Shield);

        }

        protected override void OnShow(UIBaseData data)
        {
            ChangeChildUI();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            InitTabsUnitSelected();
        }



        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds
        private void ChangeChildUI(int index = 1)
        {
            CloseCurrUI(Switch(IndexCurrTab));
            IndexCurrTab = index;
            Switch(index).Show();
        }
        private UIBaseNode Switch(int i)
        {
            switch ((FriendSystemType)i)
            {
                case FriendSystemType.friendList:
                    return uiFriendList;
                case FriendSystemType.friendApplication:
                    return uiFriendApplication;
                case FriendSystemType.friendSearch:
                    return uIFriend_Search;
                case FriendSystemType.friendShield:
                    return uIFriendShield;
                default:
                    return null;
            }
        }

        private void InitTabsUnitSelected()
        {
            tabs = this.ui.toggleGroup.GetComponentsInChildren<ExToggle>().ToList();
            for (int i = 0; i < tabs.Count; i++)
            {
                int index = i + 1;
                tabs[i].onValueChanged.AddListener((isOn) =>
                {
                    if (isOn)
                    {
                        ChangeChildUI(index);
                    }
                });
            }
        }
        private void CloseCurrUI(UIBaseNode currTab)
        {
            if (currTab == null) return;
            currTab.Hide();
        }


        #endregion methonds
    }
}